package topevery.syn.data.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import topevery.syn.data.model.base64.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class UtilsHelper {

    /**
     * 是否为空字符串
     *
     * @param val 要判断的字符串
     * @return ""
     */
    public static boolean stringIsEmpty(String val) {
        return val == null || "".equals(val);
    }

    /**
     * 压缩字符串
     *
     * @param data
     * @return
     */
    public static String stringCompress(String data) {
        if (stringIsEmpty(data)) {
            return data;
        }

        byte[] dataGzipBytes = null;
        {
            ByteArrayOutputStream outputStream = null;
            GZIPOutputStream gzipStream = null;
            try {
                outputStream = new ByteArrayOutputStream();
                gzipStream = new GZIPOutputStream(outputStream);
                gzipStream.write(data.getBytes());
                gzipStream.finish();
                dataGzipBytes = outputStream.toByteArray();
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                ioClose(gzipStream, outputStream);
            }
        }
        if (dataGzipBytes != null) {
            return Base64.encodeBase64String(dataGzipBytes);
        } else {
            return null;
        }
    }

    /**
     * 解压缩字符串
     *
     * @param data
     * @return
     */
    public static String stringDecompress(String data) {
        if (stringIsEmpty(data)) {
            return data;
        }

        String res = null;
        {
            byte[] gzipBytes = Base64.decodeBase64(data);

            ByteArrayInputStream inputStream = null;
            GZIPInputStream unGzipStream = null;
            ByteArrayOutputStream outputStream = null;
            try {
                inputStream = new ByteArrayInputStream(gzipBytes);
                unGzipStream = new GZIPInputStream(inputStream);
                outputStream = new ByteArrayOutputStream();

                byte[] buffer = new byte[256];
                int readLength;
                while ((readLength = unGzipStream.read(buffer)) >= 0) {
                    outputStream.write(buffer, 0, readLength);
                }
                outputStream.flush();
                res = outputStream.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                ioClose(unGzipStream, outputStream, inputStream);
            }
        }
        return res;
    }

    /**
     * 关闭io
     *
     * @param closeables
     */
    public static void ioClose(Closeable... closeables) {
        for (Closeable i : closeables) {
            if (i != null) {
                try {
                    i.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    /**
     * json序列化
     *
     * @param val
     * @param <T>
     * @return ""
     */
    public static <T> String jsonSerialize(T val) {
        return JSON.toJSONString(val);
    }

    /**
     * json反序列化
     * jsonDeserialize(str,RsContent.class);
     *
     * @param jsonData
     * @param <T>
     * @return ""
     */
    public static <T> T jsonDeserialize(String jsonData, Class<T> clazz) {
        return JSON.parseObject(jsonData, clazz);
    }

    public static String stringCompressJson(Object val) {
        String jsonString = JSON.toJSONString(val);
        return stringCompress(jsonString);
    }

    public static <T> T stringDecompressJson(String val, Class<T> clazz) {
        String jsonString = stringDecompress(val);
        return JSON.parseObject(jsonString, clazz);
    }

    /**
     * stringDecompressJson(ivkRes, new TypeReference<List<EquipmentInfo>>(){})
     * **/
    public static <T> T stringDecompressJson(String val,  TypeReference<T> type) {
        String jsonString = stringDecompress(val);
        return JSON.parseObject(jsonString, type);
    }

    public static <T> T assemblyCreateInterface(Class<T> clazz, String className) {
        try {
            if (stringIsEmpty(className)) {
                return null;
            }

            String[] strs = className.split(",");
            if (strs.length == 2) {
                className = strs[0];
            }
            Class<T> clz = (Class<T>) Class.forName(className);
            Object obj = clz.newInstance();
            //需要检查checkType是不是obj的字节码对象
            if (!clazz.isInstance(obj)) {
                throw new Exception("对象跟字节码不兼容");
            }
            return (T) obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}